package ED2;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;

public final class LeiuraDados
{   
    private static HashMap<Integer, AlunoBean> data;
    
    public LeiuraDados()
    {
        
    }
    
    public LeiuraDados(String rootPath)
    {
        read(rootPath);
    }
    
    public HashMap<Integer, AlunoBean> getData()
    {
        return data;
    }
    
    private void read(String path)
    {
        ArrayList<File> files = readDir(path);
        
        if(files.size() > 0)
        {
            data = new HashMap<Integer, AlunoBean>();
            
            for(File file : files)
            {   
                int ownerRm = Integer.valueOf(getFileNameWithoutExtension(file));
                int[] friendsRms = parseFileContent(lerArquivo(file));
                
                AlunoBean student = new AlunoBean(ownerRm, friendsRms);
                
                data.put(ownerRm, student);
                
                System.out.println(" Lendo Arquivo : " + file.getName());
                
                for(int rm : friendsRms)
                {
                    System.out.println(" Rm: " + rm);
                }
                
               
            }
        }
    }
    
  
    private ArrayList<File> readDir(final String dirPath)
    {
        ArrayList<File> files = new ArrayList<File>();
        
        File dir = new File(dirPath);
        
        for(final File fileEntry : dir.listFiles())
        {
            if(fileEntry.isFile())
            {
                files.add(fileEntry);
            }
        }
        
        return files;
    }
    
    
    private String lerArquivo(final File file)
    {
        String fileContent = "";
        BufferedReader bufferedReader = null;
        
        try
        {
            bufferedReader = new BufferedReader(new FileReader(file));
            
            String line;
            
            while((line = bufferedReader.readLine()) != null)
            {
                fileContent += line;
            }
        }
        catch(Exception exception)
        {   
            System.out.println("LerDados lerArquivo() Exception: " + exception.getMessage());
        }
        finally
        {
            try
            {
                bufferedReader.close();
            }
            catch(Exception exception)
            {
                System.out.println("LerDados lerArquivo() Exception: " + exception.getMessage());
            }
        }
        
        return fileContent;
    }
    
    private String getFileNameWithoutExtension(File file)
    {
        String fileNameWithoutExtension;
        String fileName = file.getName();
        
        fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf("."));
        
        return fileNameWithoutExtension;
    }
    
    private int[] parseFileContent(String fileContent)
    {
        if(fileContent.length() > 0)
        {
            if(fileContent.indexOf(",") > -1)
            {
                String[] rmsAsString = fileContent.split(",");
                
                int[] rms = new int[rmsAsString.length];
                
                for(int i = 0; i < rmsAsString.length; i++)
                {
                    rms[i] = Integer.valueOf(rmsAsString[i].trim());
                }
                
                return rms;
            }
            else
            {
                return new int[]{Integer.valueOf(fileContent.trim())};
            }
        }
        else
        {
            return new int[]{};
        }
    }
}
